package com.neuedu.yunong.service.impl;
import lombok.RequiredArgsConstructor;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.neuedu.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.neuedu.yunong.domain.YunongFamilyMembers;
import com.neuedu.yunong.mapper.YunongFamilyInfoMapper;
import com.neuedu.yunong.domain.YunongFamilyInfo;
import com.neuedu.yunong.service.IYunongFamilyInfoService;

/**
 * 家庭经济Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-13
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class YunongFamilyInfoServiceImpl extends ServiceImpl<YunongFamilyInfoMapper, YunongFamilyInfo> implements IYunongFamilyInfoService
{
    private final YunongFamilyInfoMapper yunongFamilyInfoMapper;

    /**
     * 查询家庭经济
     * 
     * @param id 家庭经济主键
     * @return 家庭经济
     */
    @Override
    public YunongFamilyInfo selectYunongFamilyInfoById(Long id)
    {
        return yunongFamilyInfoMapper.selectYunongFamilyInfoById(id);
    }

    /**
     * 查询家庭经济列表
     * 
     * @param yunongFamilyInfo 家庭经济
     * @return 家庭经济
     */
    @Override
    public List<YunongFamilyInfo> selectYunongFamilyInfoList(YunongFamilyInfo yunongFamilyInfo)
    {
        return yunongFamilyInfoMapper.selectYunongFamilyInfoList(yunongFamilyInfo);
    }

    /**
     * 新增家庭经济
     * 
     * @param yunongFamilyInfo 家庭经济
     * @return 结果
     */
    @Transactional
    @Override
    public int insertYunongFamilyInfo(YunongFamilyInfo yunongFamilyInfo)
    {
        int rows = yunongFamilyInfoMapper.insertYunongFamilyInfo(yunongFamilyInfo);
        insertYunongFamilyMembers(yunongFamilyInfo);
        return rows;
    }

    /**
     * 修改家庭经济
     * 
     * @param yunongFamilyInfo 家庭经济
     * @return 结果
     */
    @Transactional
    @Override
    public int updateYunongFamilyInfo(YunongFamilyInfo yunongFamilyInfo)
    {
        yunongFamilyInfoMapper.deleteYunongFamilyMembersByFamilyId(yunongFamilyInfo.getId());
        insertYunongFamilyMembers(yunongFamilyInfo);
        return yunongFamilyInfoMapper.updateYunongFamilyInfo(yunongFamilyInfo);
    }

    /**
     * 批量删除家庭经济
     * 
     * @param ids 需要删除的家庭经济主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYunongFamilyInfoByIds(Long[] ids)
    {
        yunongFamilyInfoMapper.deleteYunongFamilyMembersByFamilyIds(ids);
        return yunongFamilyInfoMapper.deleteYunongFamilyInfoByIds(ids);
    }

    /**
     * 删除家庭经济信息
     * 
     * @param id 家庭经济主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYunongFamilyInfoById(Long id)
    {
        yunongFamilyInfoMapper.deleteYunongFamilyMembersByFamilyId(id);
        return yunongFamilyInfoMapper.deleteYunongFamilyInfoById(id);
    }

    /**
     * 新增家庭成员经济信息
     * 
     * @param yunongFamilyInfo 家庭经济对象
     */
    public void insertYunongFamilyMembers(YunongFamilyInfo yunongFamilyInfo)
    {
        List<YunongFamilyMembers> yunongFamilyMembersList = yunongFamilyInfo.getYunongFamilyMembersList();
        Long id = yunongFamilyInfo.getId();
        if (StringUtils.isNotNull(yunongFamilyMembersList))
        {
            List<YunongFamilyMembers> list = new ArrayList<YunongFamilyMembers>();
            for (YunongFamilyMembers yunongFamilyMembers : yunongFamilyMembersList)
            {
                yunongFamilyMembers.setFamilyId(id);
                list.add(yunongFamilyMembers);
            }
            if (list.size() > 0)
            {
                yunongFamilyInfoMapper.batchYunongFamilyMembers(list);
            }
        }
    }
}
